package com.niannian.util.network;

import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.util.EntityUtils;
import android.util.Log;

public class NetRequest2 {
	public NetRequest2() {

	}

	/**
	 * Do sync request.
	 * 
	 * @param url
	 *            The full url that needs to be signed including its non OAuth
	 *            url parameters
	 * @param httpMethod
	 *            The http method used. Must be a valid HTTP method verb
	 *            (POST,GET,PUT, etc)
	 * @param key
	 *            OAuth key
	 * @param listParam
	 *            Query parameters
	 * @param listFile
	 *            Files for post
	 * @return
	 * @throws Exception
	 */

	public String syncRequest(String url, String httpMethod, List<NameValuePair> listParam, List<NameValuePair> listFile) {
		return syncRequest(url, httpMethod, listParam, listFile, 0, false);
	}

	public String syncRequest(String url, String httpMethod, List<NameValuePair> listParam, List<NameValuePair> listFile, Boolean authtoken) {
		return syncRequest(url, httpMethod, listParam, listFile, 0, authtoken);
	}

	public String syncRequest(String url, String httpMethod, List<NameValuePair> listParam, List<NameValuePair> listFile, int timeout, Boolean authtoken) {
		if (url == null || "".equals(url)) {
			Log.i("url", "error url:null");
			return null;
		}

		String reValue = null;
		try {
			HttpService service = new HttpService();
			HttpResponse response = null;
			if ("GET".equals(httpMethod)) {
				response = service.sendGetRequest(listParam, url);
			} else if ("DELETE".equals(httpMethod)) {
				response = service.sendDeleteRequest(listParam, url);
			} else if ("PUT".equals(httpMethod)) {
				if ((listFile == null) || (listFile.size() == 0)) {
					response = service.sendPutRequest(listParam, url);
				} else {
					response = service.sendPutRequest(listParam, listFile, url);
				}
			} else if ("POST".equals(httpMethod)) {
				if ((listFile == null) || (listFile.size() == 0)) {
					response = service.sendPostRequest(listParam, url);
				} else {
					response = service.sendPostRequest(listParam, listFile, url);
				}
			}

			if (response.getStatusLine().getStatusCode() == 403) {
				if (service.setXToken()) {
					if ("GET".equals(httpMethod)) {
						response = service.sendGetRequest(listParam, url);
					} else if ("DELETE".equals(httpMethod)) {
						response = service.sendDeleteRequest(listParam, url);
					} else if ("PUT".equals(httpMethod)) {
						if ((listFile == null) || (listFile.size() == 0)) {
							response = service.sendPutRequest(listParam, url);
						} else {
							response = service.sendPutRequest(listParam, listFile, url);
						}
					} else if ("POST".equals(httpMethod)) {
						if ((listFile == null) || (listFile.size() == 0)) {
							response = service.sendPostRequest(listParam, url);
						} else {
							response = service.sendPostRequest(listParam, listFile, url);
						}
					}
				} else {
					Log.i("activity", "Fail to update xToken!");
				}
			}

			if (authtoken) {
				service.setXToken2();
			}

			if (response.getStatusLine().getStatusCode() == 200) {
				try {
					return EntityUtils.toString(response.getEntity(), "UTF-8");

				} catch (Exception e) {
					Log.i("activity", "Exception catched: " + e.toString());
				}
			} else {
				if (response.getStatusLine().getStatusCode() == 408) {
					reValue = "CONNECT_TIMEOUT";
				} else if (response.getStatusLine().getStatusCode() == 404) {
					reValue = "CONNECT_FAIL";
				} else {
					reValue = "CONNECT_EXC";
				}
			}

		} catch (Exception e) {
			reValue = "CONNECT_EXC";
			e.printStackTrace();
			Log.e("x", e.toString());
			// Common.tip(ctx,"网络连接异常");
		}
		return reValue;
	}
}
